home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dosutils.zip / ATTRIB.C next >
Text File  |  1994-07-04  |  3KB  |  165 lines

  1. /*
  2.  *  attrib - Change file attributes
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stddef.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <io.h>
  28. #include <dir.h>
  29. #include <dos.h>
  30.  
  31. static char *
  32. expand_wildcards(char *s)
  33. {
  34.     static    struct find_t info;
  35.     char wildcard[80];
  36.     static char directory[256];
  37.     static char filename[256];
  38.     unsigned attrib;
  39.     char *c1, *c2;
  40.  
  41.     if (s)
  42.     {
  43.         strcpy(wildcard, s);
  44.         strcpy(directory, wildcard);
  45.             if ((c1 = strrchr(directory, '/')) != 0 ||
  46.             (c2 = strrchr(directory, '\\')) != 0)
  47.             {
  48.             if (c1 > c2)
  49.                 c1[1] = '\0';
  50.             else
  51.                 c2[1] = '\0';
  52.         }
  53.         else
  54.             directory[0] = '\0';
  55.         if (! strchr(wildcard, '.'))
  56.             strcat(wildcard, "*.*");
  57.         attrib = _A_HIDDEN | _A_HIDDEN | _A_ARCH | _A_NORMAL | _A_SUBDIR | _A_RDONLY;
  58.         if (_dos_findfirst(wildcard, attrib, &info) != 0)
  59.             return NULL;
  60.         while (!strcmp(info.name, ".") || !strcmp(info.name, ".."))
  61.             if (_dos_findnext(&info))
  62.                 return NULL;
  63.     }
  64.     else
  65.     {
  66.         if (_dos_findnext(&info) != 0)
  67.             return NULL;
  68.     }
  69.     strcpy(filename, directory);
  70.     strcat(filename, info.name);
  71.     return filename;
  72. }
  73.  
  74. int
  75. main(    int    argc,
  76.     char    **argv)
  77. {
  78.     char    *pchFile;
  79.     int    on = 0;
  80.     int    off = 0;
  81.     int    turn_on;
  82.     unsigned value;
  83.  
  84.     while (--argc)
  85.     {
  86.         argv++;
  87.         if (**argv == '-' ||
  88.             **argv == '+')
  89.         {
  90.             while (**argv)
  91.             {
  92.                 switch(**argv)
  93.                 {
  94.                 case '+':
  95.                     turn_on = 1;
  96.                     value = 0;
  97.                     break;
  98.  
  99.                 case '-':
  100.                     turn_on = 0;
  101.                     value = 0;
  102.                     break;
  103.  
  104.                 case 'r':
  105.                 case 'R':
  106.                     value = _A_RDONLY;
  107.                     break;
  108.  
  109.                 case 'a':
  110.                 case 'A':
  111.                     value = _A_ARCH;
  112.                     break;
  113.  
  114.                 case 's':
  115.                 case 'S':
  116.                     value = _A_SYSTEM;
  117.                     break;
  118.  
  119.                 case 'h':
  120.                 case 'H':
  121.                     value = _A_HIDDEN;
  122.                     break;
  123.                     
  124.                 default:
  125.                     fprintf(stderr, "Usage: attrib [{+-}{ahrs}] file ...\n");
  126.                     return 1;
  127.                 }
  128.                 if (turn_on)
  129.                 {
  130.                     on |= value;
  131.                     off &= ~value;
  132.                 }
  133.                 else
  134.                 {
  135.                     off |= value;
  136.                     on &= ~value;
  137.                 }
  138.                 ++*argv;
  139.             }
  140.         }
  141.         else
  142.         {
  143.             for (pchFile = expand_wildcards(*argv);
  144.                  pchFile;
  145.                  pchFile = expand_wildcards(0))
  146.             {
  147.                 if (_dos_getfileattr(pchFile, &value))
  148.                 {
  149.                     fprintf(stderr, "%s: %s\n", pchFile, sys_errlist[errno]);
  150.                     return 1;
  151.                 }
  152.                 value &= ~off;
  153.                 value |= on;
  154.                 if (_dos_setfileattr(pchFile, value))
  155.                 {
  156.                     fprintf(stderr, "%s: %s\n", pchFile, sys_errlist[errno]);
  157.                     return 1;
  158.                 }
  159.             }
  160.         }
  161.     }
  162.     return 0;
  163. }
  164.  
  165.